package rsee.com;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.video.h264.SqlHelper;

import ezeye.device.EyeDeviceInfo;
import ezeye.device.EyeDeviceManager;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class RecordDialog {
	public static final int mButtonAdd = 0x10;
	public static final int mButtonCommit = 0x11;
	public static final int mButtonPlayAll = 0x12;
	
	public interface RecordDialogCallBack {		
		void onRecordDialogButtonClick(int buttonId);
	}
	
	public static void allPlay(int recordId)
	{
    	String mcurrent = mRecordNames[recordId];   	
    	mChoosedRecord = mcurrent;
	    if (null != mListener) {
			mListener.onRecordDialogButtonClick(mButtonPlayAll);
		}
	}
	
	private class TouchHandler implements Button.OnClickListener, OnChildClickListener {
		@Override
		public void onClick(View v) {
			if (v == mAddButton) {
				if (null != mListener) {
					mListener.onRecordDialogButtonClick(mButtonAdd);
				}
			}
		}

		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			
	    	int mchannel = childPosition;
	    	String mcurrent = mRecordNames[groupPosition];
	    	
	    	mChoosedRecord = mcurrent;
	    	mChoosedChannel = mchannel;
			
	    	if (null != mListener) {
				mListener.onRecordDialogButtonClick(mButtonCommit);
			}
	    	
			return true;
		}
	}
	
	public class ShowType {
		// TODO TypeUnknown
		public static final int TypeUnknown = 3;
		public static final int TypePad = 7;
		public static final int TypeList = 8;
		public static final int TypeLoad = 9;
	}
	
	private Context mContext = null;
	private AlertDialog mDialog = null;
	private View mListView = null;
	private TouchHandler mTouchHandler = null;
	private static RecordDialogCallBack mListener = null;
	private Button mAddButton = null;
	private ExpandableListView mList = null;
	
	private ExAdapter mAdapter = null;
	private static String mRecordNames[] = null;
	private int mRecordChanNums[] = null;
	
	private int mShowType = 0;
	
	private static String mChoosedRecord = null;
	private int mChoosedChannel = -1;

	public RecordDialog(Context context, RecordDialogCallBack listener) {
		mContext = context;
		mListener = listener;

		//mDialog = new AlertDialog.Builder(mContext).create();
		//mDialog.setContentView(mView);

		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		mListView = View.inflate(mContext, R.layout.list, null);
		builder.setView(mListView);
		mDialog = builder.create();
		
		mTouchHandler = new TouchHandler();
		mAddButton = (Button)mListView.findViewById(R.id.addButton);
		mAddButton.setOnClickListener(mTouchHandler);
		
		mAdapter = new ExAdapter(mContext);
		setShowType(ShowType.TypePad);
		
        mList = (ExpandableListView)mListView.findViewById(R.id.lvDynamic);
		mList.setAdapter(mAdapter);
		mList.setGroupIndicator(null);
		mList.setDivider(null);
		mList.setOnChildClickListener(mTouchHandler);
	}
	
	public void layout(int l, int t, int r, int b) {
		// TODO layout
		//AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(r-l, b-t, l, t);
		//mDialog.setContentView(mListView, params);
	}
	
	public boolean isShowing() {
	    if (null == mDialog)
	        return false;
	    
	    boolean result = mDialog.isShowing();
	    return result;
	}
	
	public void show() {	
		if (mShowType == ShowType.TypeUnknown || mShowType == ShowType.TypePad) {
			mAddButton.setVisibility(View.GONE);
		}
		
		if (!mDialog.isShowing())
			mDialog.show();
	}
	
	public void cancel() {
		if (mDialog.isShowing())
			mDialog.cancel();
	}
	
	public String getChoosedRecord() {
		return mChoosedRecord;
	}
	
	public int getChoosedChannel() {
		return mChoosedChannel;
	}

	//================ Data ================
	public void setShowType(int showType) {
		mShowType = showType;
		if (null != mAdapter)
			mAdapter.setShowType(showType);
	}
		
	public void fetchData(SqlHelper sqlHelper) {
		mAdapter.clear();

		String[] recordList = sqlHelper.getList();
		if (recordList != null) {
			mRecordNames = recordList;
			mRecordChanNums = new int[recordList.length];

			mAdapter.setRecordNames(mRecordNames);
			mAdapter.setRecordChanNums(mRecordChanNums);

			for (int i = 0; i < recordList.length; i++) {
				String ip = null;
				int channelCount = 0;
				Cursor cursor = sqlHelper.getCurosr(String.format(
						"recordname=\"%s\"", recordList[i]));
				if (cursor.moveToFirst()) {
					//saveInfo(cursor);
					channelCount = cursor.getInt(6);
					mRecordChanNums[i] = channelCount;
					ip = cursor.getString(3);
					cursor.close();
				}
				Map<String, String> curGroupMap = new HashMap<String, String>();
				mAdapter.addGroupData(curGroupMap);
				curGroupMap.put(ExAdapter.G_TEXT, recordList[i] + "[" + ip
						+ "]");

				List<Map<String, String>> children = new ArrayList<Map<String, String>>();
				if (mShowType != ShowType.TypeLoad) {
					for (int j = 0; j < channelCount; j++) {
						Map<String, String> curChildMap = new HashMap<String, String>();
						children.add(curChildMap);
						curChildMap.put(ExAdapter.C_TEXT1, mContext
								.getResources().getString(R.string.tongdao)
								+ (j + 1));
					}
				}
				mAdapter.addChildData(children);
			}
		}
	}
	
	  private void saveInfo(Cursor cursor)
	    {
	    	  EyeDeviceManager deviceMgr = EyeDeviceManager.getInstance();
	          assert (deviceMgr.isLoaded());
	          EyeDeviceInfo info = deviceMgr.getDeviceInfo(cursor.getString(0));
	          if (null == info)
	          {	
	        	 Log.e("RecordDialog", "addInfo");
	          	info = deviceMgr.createDeviceInfo(cursor.getString(0));
	          	info.setUser(cursor.getString(1));
	          	info.setPassword(cursor.getString(2));
	          	info.setHost(cursor.getString(3));
	          	info.setPort(cursor.getInt(4));
	          	info.setChanDefault((byte)cursor.getInt(5));
	          	info.setChanTotal((byte)cursor.getInt(6));
	          	info.setAlarmOpen(cursor.getInt(7));
	          	info.setAlarmCount(cursor.getInt(8));
	          	info.setDeviceInfo(cursor.getString(9));
	          	info.setVeri(cursor.getInt(10));
	          	info.setVersion(cursor.getInt(11));
	            deviceMgr.saveStore(cursor.getString(0));
	          }
	    }
	  
	  
}
